package org.bbl.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Properties;

/**
 * @author biniamg
 * @since 7/12/12
 */
public class ProjectResources {
    private static final String ROOT =  System.getProperty("ATFHOME");
    private static final String DEFAULT_ROOT = ROOT == null ? "target" : ROOT;

    public static File getProjectRoot0() {
        File file = new File(ProjectResources.class.getResource("/").getFile());
        while (!file.getAbsolutePath().endsWith(DEFAULT_ROOT)
                || file.getAbsolutePath().indexOf(DEFAULT_ROOT) != file.getAbsolutePath().lastIndexOf(DEFAULT_ROOT)) {
            file = file.getParentFile();
        }
        return file.getParentFile();
    }

    public static File getProjectRoot() {
        return ROOT == null ? getProjectRoot0() : new File(ROOT);
    }

    public static File getResourceFile(String... dir) {
        return chainDirs(getResourceDir(), dir);
    }

    public static File getAtfRoot() {
        return chainDirs(new File(System.getProperty("user.home"), "MDPATF"));
    }


    public static File getResourceDir() {
        return chainDirs(getProjectRoot(), "src", "main", "resources");
    }

    public static File getConfigDir(String... dir) {
        return chainDirs(new File(getAtfRoot(), "config"), dir);
    }

    public static File getContinuousDir(String... dir) {
        return chainDirs(new File(getAtfRoot(), "continuous"), dir);
    }


    public static File getCronDir() {
        return new File(getAtfRoot(), "cronFiles");
    }

    public static File getRunOnceDir(String... dir) {
        return chainDirs(new File(getAtfRoot(), "once"), dir);
    }

    public static File getTestResourceFile(String... dir) {
        return chainDirs(getTestResourceDir(), dir);
    }

    public static File getTestResourceDir() {
        return chainDirs(getProjectRoot(), "src", "test", "resources");
    }

    public static String getBuildNumber() {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(getResourceFile("build.properties")));
            return properties.getProperty("atf.build.number");
        } catch (IOException e) {
            return "unknown";
        }
    }

    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            return "unknown";
        }
    }

    public static Date getBuildDate() {
        return new Date(getResourceFile("build.properties").lastModified());
    }

    public static File chainDirs(File parent, String... dirs) {
        File result = parent;
        for (String s : dirs) {
            result = new File(result, s);
        }
        return result;
    }
}
